Completed
Push — master ( 319937...0561bf )
by Wallace
01:29
created

movie.test.js ➔ describe(ꞌMovieꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 74
rs 9.0335
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Test movie
3
 */
4
5
const ServerMock = require('mock-http-server')
6
const expect = require('chai').expect
7
const movie = require('../bin/movies')
8
9
const serve = new ServerMock({
10
  host: 'localhost',
11
  port: 3000
12
})
13
14
const req = () => {
15
  serve.on({
16
    method: 'GET',
17
    path: '/response',
18
    reply: {
19
      status: 200,
20
      headers: {'content-type': 'application/json'},
21
      body: JSON.stringify({
22
        'data': {
23
          'movies': [
24
            {
25
              'id': 1,
26
              'title': 'Star Wars',
27
              'cover': 'Movie cover',
28
              'rating': '9.7',
29
              'genres': ['Action'],
30
              'torrents': [
31
                {
32
                  'quality': '3D'
33
                }
34
              ]
35
            }
36
          ]
37
        }
38
      })
39
    }
40
  })
41
}
42
43
describe('Movie', function () {
44
  beforeEach(function (done) {
45
    serve.start(done)
46
  })
47
48
  afterEach(function (done) {
49
    serve.stop(done)
50
  })
51
52
  before(function () {
53
    movie.setEndpoint({list: 'http://localhost:3000/response'})
54
  })
55
56
  it ('expect to be an error if endpoint is not an object', function () {
57
    expect(() => {
58
      movie.setEndpoint('string')
59
    }).to.throw('endpoint must be an object')
60
  })
61
62
  it ('expect to be an error if param is not an object', function () {
63
    expect(() => {
64
      movie.setParams('string')
65
    }).to.throw('params must be an object')
66
  })
67
68
  it ('expect return an array', function () {
69
    req()
70
71
    return movie.get()
72
      .then(response => {
73
        expect(response.data.movies).to.be.an('array')
74
      })
75
  })
76
77
  it ('expect return an array and contains genres with action property', function () {
78
    req()
79
80
    return movie.findByGenre('action')
81
      .then(function (response) {
82
        expect(response.data.movies[0])
83
          .to.be.an('object')
84
          .to.have.deep.property('genres[0]', 'Action')
85
      })
86
  })
87
88
  it ('expect return an array and contains quality with 3D property', function () {
89
    req()
90
91
    return movie.findByQuality('3D')
92
      .then(function (response) {
93
        expect(response.data.movies[0])
94
          .to.be.an('object')
95
          .to.have.deep.property('torrents[0].quality', '3D')
96
      })
97
  })
98
99
  it ('expect return an array and contain rating higher than argument', function () {
100
    req()
101
102
    return movie.findByRating('8')
103
      .then(function (response) {
104
        expect(response.data.movies[0].rating).to.be.above(8)
105
      })
106
  })
107
108
  it ('expect return to be an array and have title match with Star Wars', function () {
109
    req()
110
111
    return movie.search('Star Wars')
112
      .then(response => {
113
        expect(response.data.movies[0].title).to.match(/Star Wars/g)
114
      })
115
  })
116
})
117